<template>
<div class="home">
  <el-container>
    <el-header class="headers">
      <div class="headers-title">云e办</div>
      <div>
        <el-dropdown class="userInfo"  @command="handleCommand">
  <span>
   <span>{{user.name}}</span>
    <i><img src="../assets/img/tx.jpg" alt="" class="tx"></i>
  </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="userCenter">个人中心</el-dropdown-item>
            <el-dropdown-item command="setting">设置</el-dropdown-item>
            <el-dropdown-item command="logout">注销登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu router unique-opened>
        <el-submenu :index="index +''"
                    v-for="(item,index) in routes"
                          :key="index"
                    v-if="!item.hidden">
          <template slot="title">
            <i :class="item.iconCls" style="margin-right: 5px;color: #2b91af;"></i>
            {{item.name}}</template>
            <el-menu-item
                :index="children.path"
                v-for="(children,index) in item.children"
            :key="index">
             <span>{{children.name}}</span>
            </el-menu-item>
        </el-submenu>
          </el-menu>
      </el-aside>
      <el-main>
        <el-breadcrumb separator-class="el-icon-arrow-right" >
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{this.$router.currentRoute.name}}</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="homePage" v-if="this.$router.currentRoute.path === '/home'">欢迎来到云e办^~^</div>
        <router-view class="routerView"/>
      </el-main>
    </el-container>
  </el-container>
</div>
</template>

<script>
import {postRequest} from "../utils/api";

export default {
name: "Home",
  data(){
  return{
     user:JSON.parse(window.sessionStorage.getItem('user'))
  }
  },
  methods:{
    handleCommand(command){
      if(command==='logout'){
        this.$confirm('是否要注销登录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //注销登录
         postRequest('/logout')
          //清空本地缓存
          window.sessionStorage.removeItem('tokenStr')
          window.sessionStorage.removeItem('user')
          //清空菜单
          this.$store.commit('initRoutes',[])
          //跳转路由
          this.$router.replace('/')
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消注销'
          });
        });
      }
    }
  },
  computed:{
  routes(){
    return this.$store.state.routes
  }
  }
}
</script>

<style scoped>
.headers{
  justify-content: space-between;
  font-size: 30px;
  background: #2b91af;
  display: flex;
  color: #FFFFFF;
  font-family: 华文行楷;
  align-items: center;
  padding: 0 15px;
  box-sizing: border-box;
}
.userInfo{
  cursor: pointer;
}
.tx{
  width: 48px;
  height: 48px;
  border-radius: 24px;
  margin-left: 8px;
}
.homePage{
  font-size: 40px;
  padding: 150px 200px;
  color: #209bc2;
}
.routerView{
  margin-top: 20px;
}
</style>